package org.shoukaiseki.aliyun.oss.config;

import org.springframework.util.Assert;

import java.util.Locale;

/**
 * org.shoukaiseki.aliyun.oss.config.BaseOSSConfigFactory
 * <br>
 *
 * @author shoukaiseki ブログ http://www.shoukaiseki.top/<br> E-メール jiang28555@Gmail.com<br>
 * @date 2020-11-18 15:40:52<br>
 */
public class BaseOSSConfigFactory {

    private String savePath;

    private long fileMaxSize=-1L;

    private String fileMaxSizeString="-1";

    public String getSavePath() {
        return savePath;
    }

    public void setSavePath(String savePath) {
        this.savePath = savePath;
    }

    public long getFileMaxSize() {
        return fileMaxSize;
    }

    public void setFileMaxSize(String fileMaxSize) {
        this.fileMaxSize = this.parseSize(fileMaxSize);
        this.fileMaxSizeString=fileMaxSize;
    }


    private long parseSize(String size) {
        Assert.hasLength(size, "Size must not be empty");
        size = size.toUpperCase(Locale.ENGLISH);
        if (size.endsWith("KB")) {
            return Long.valueOf(size.substring(0, size.length() - 2)) * 1024L;
        } else {
            return size.endsWith("MB") ? Long.valueOf(size.substring(0, size.length() - 2)) * 1024L * 1024L : Long.valueOf(size);
        }
    }

    public BaseOSSConfigElement createMultipartConfig() {
        return new BaseOSSConfigElement(this.savePath,this.fileMaxSize,this.fileMaxSizeString);
    }
}
